package com.upcode.weighing.admin.utils;

import com.upcode.weighing.util.ops.OtherUtills;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class StatisticTimeUtil {


    /**
     * 时间搜索
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param types //类型 1:今日 2:昨日 3:最近7天 4:最近30天
     * @return
     */
    public static String[] time8(String startTime,String endTime,Integer types) throws ParseException {
        if (types==null&&!OtherUtills.checkStr(startTime)&&!OtherUtills.checkStr(endTime)){
            throw new RuntimeException("请选择时间");
        }
        String[] res=null;
        if(OtherUtills.checkStr(startTime)&&OtherUtills.checkStr(endTime)){
            int i = differentDays(startTime, endTime);
            if (i>365){
                throw new RuntimeException("选择时间超过一年365天");
            }
            if (i<3){
                throw new RuntimeException("选择时间不得小于三天");
            }
            res=new String[]{startTime,endTime};
            return res;
        }
        if(types==null){
            res=new String[]{"",""};
            return res;
        }
        //类型 1:今日 2:昨日 3:最近7天 4:最近30天
        if(types==1){
            throw new RuntimeException("无法选择今天");
        }else if(types==2){
            throw new RuntimeException("无法选择昨天");
        }else if(types==3){
            res=getLastSevenDays(6);
        }else if(types==4){
            res=getLastSevenDays(29);
        }
        return res;
    }

    /**
     * 获取近七天起始日期和结束日期
     * @return
     */
    public static String[] getLastSevenDays(int index){
        String startTime=time7(index)+" 00:00:00";
        String endTime=time3();
        return new String[]{startTime,endTime};
    }


    /**
     * 获取日期
     * @param index
     * @return
     */
    public static String time7(Integer index){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-index);
        String res = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
        return res;
    }

    /**
     * 获取当前时间转为 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String time3(){
        LocalDateTime time=LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.format(time);
    }

    /**
     * date2比date1多的天数
     * @param time1
     * @param time2
     * @return
     */
    public static int differentDays(String time1,String time2) throws ParseException {
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(time1);
        Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(time2);
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0) //闰年
                {
                    timeDistance += 366;
                }
                else //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else //不同年
        {
            return day2-day1;
        }
    }

}
